<?php
/**
 * Created by PhpStorm.
 * User: alice
 * Date: 2015/12/26
 * Time: 14:14
 */

/**
 * Class Involved_ProjectModel
 */

class Involved_InvestRecordModel extends BaseModel
{

    protected $table = 'invest_record';

    /**
     * 获取投资记录列表
     */
    public function InvestRecordList($project_id,$page){
            $offset = ($page-1) * HostEnum::PAGE_NUM;
            return DB::table($this->table)
                ->select('user_id','invest_money','status','invest_time')
                ->where([ 'project_id'=>$project_id])
                ->skip($offset)->take(HostEnum::PAGE_NUM)
                ->get();

    }


    /**
     * 获取投资记录列表
     */
    public function GetInvestRecordOne($id){
        return  DB::table($this->table)
            ->where('invest_record.invest_id',$id)
            ->first();
    }
    /**
     * 投资总收益
     */
    public function AllInvestRecord(){
        return  DB::table($this->table)
            ->sum('yield_money');
    }

    /**
     * 已经投资金额
     */
    public function AlreadyMoney($id){
        $id = implode(',',$id);
        return  DB::select("select sum(invest_money) as money,project_id from ypt_invest_record where project_id IN($id) GROUP BY project_id ");
    }


    /**
     * 投资数
     */
    public function AllInvestNum($project_id=null){
        if(empty($project_id)) {
            return DB::table($this->table)->count();
        }else{
            return DB::table($this->table)->where('project_id',$project_id)->count();
        }

    }

    /**
     * app投标记录查询
     * @param $page
     * @param $project_id
     * @return mixed
     */
    public function BidRecord($page,$project_id)
    {
        $limit = ($page-1)*10;
        return DB::table($this->table)
            ->select('real_name','invest_time','invest_money','mobile')
            ->join('user','user.user_id','=','invest_record.user_id')
            ->where(['project_id'=>$project_id])
            ->skip($limit)->take(10)
            ->get();
    }
}